<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

       <el-form-item label="工厂名字" prop="factoryName">
        <el-input
          v-model="queryParams.factoryName"
          placeholder="请输入工厂名字"
          clearable
          @keyup.enter.native="handleQuery"
        />
        </el-form-item>

        <el-form-item label="工厂联系人" prop="factoryManager">
        <el-input
          v-model="queryParams.factoryManager"
          placeholder="请输入工厂联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="服务类型" prop="factoryServiceType">
        <!-- <el-input
          v-model="queryParams.factoryIcon"
          placeholder="请输入工厂图标"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
          <el-select clearable v-model="queryParams.factoryServiceType" placeholder="请选择"  style="width:100%">
				           <el-option
                          v-for="item in serviceTypeList"
                          :key="item.code"
                          :label="item.name"
                          :value="item.code">
            </el-option>

				</el-select>

      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:factory:add']"
        >新增</el-button>
      </el-col>

     <el-col :span="1.5">
        <el-button
          type="default"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:factory:export']"
        >导出</el-button>
      </el-col>


      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:factory:remove']"
        >批量删除</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="factoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工厂id" align="center" prop="id" />
      <!-- <el-table-column label="工厂图标" align="center" prop="factoryIcon" /> -->
      <el-table-column label="工厂名称" align="center" prop="factoryName" />
      <el-table-column label="工厂类型" align="center" prop="factoryType" />
      <el-table-column label="工厂联系人" align="center" prop="factoryManager" />
      <el-table-column label="工厂联系电话" align="center" prop="factoryPhone" />
      <el-table-column label="工厂联系地址" align="center" prop="factoryAddress" />
     <el-table-column label="服务类型" align="center" prop="serviceTypeName" />
       <el-table-column label="工厂状态 " align="center" prop="status" />
      <el-table-column label="工厂日均产能" align="center" prop="capacityOfProd" />
      <el-table-column label="洗护正常时效 单位分" align="center" prop="normalWashingDuration" />
      <el-table-column label="洗护逾期时效 单位分" align="center" prop="expirationDateForCleaning" />
       <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:factory:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleStatus(scope.row,'STOP')"
            v-hasPermi="['system:factory:remove']"
            v-if="scope.row.status=='正常营业'"
          >禁用</el-button>
           <el-button
           v-if="scope.row.status=='暂停营业'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleStatus(scope.row,'NORMAL')"
            v-hasPermi="['system:factory:remove']"

          >启用</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px" label-position="right">
          <el-row>

          <el-col :span="12">
            <el-form-item label="工厂名称" prop="factoryName">
          <el-input v-model="form.factoryName" placeholder="请输入工厂名字" />
           </el-form-item>
          </el-col>



           <el-col :span="12">
           <el-form-item label="工厂类型" prop="factoryType">

                <el-select clearable v-model="form.factoryType" placeholder="请选择"  style="width:100%">
				           <el-option
                          v-for="item in factoryTypeList"
                          :key="item.code"
                          :label="item.name"
                          :value="item.code">
             </el-option>

				</el-select>


        </el-form-item>
        </el-col>

          </el-row>


   <el-row>

         <el-col :span="12">
           <el-form-item label="工厂联系人" prop="factoryManager"  >
          <el-input v-model="form.factoryManager" placeholder="请输入工厂负责人" />
        </el-form-item>
        </el-col>
          <el-col :span="12">
           <el-form-item label="工厂电话" prop="factoryPhone">
          <el-input v-model="form.factoryPhone" placeholder="请输入工厂联系电话" />
        </el-form-item>
        </el-col>
</el-row>


   <el-row>
        <el-col :span="12">
          <el-form-item label="选择地址" prop="city">


          <!-- <el-input v-model="form.factoryAddress" placeholder="请选择" /> -->
          <el-cascader
          style="width: 100%"
          size="large"
          :options="options"
          v-model="selectedOptions"
          @change="handleChange"
        >
        </el-cascader>

        </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="工厂详细地址" prop="factoryAddress"  >
          <el-input v-model="form.factoryAddress" placeholder="请输入工厂地址" />
        </el-form-item>
        </el-col>

              </el-row>
           <el-row>

          <!-- <el-col :span="24" class="warehouseReleaseRule" style="background-color: #F7F7F8; margin-bottom: 20px;">
               <div  style="
               background-color: white;
               margin-bottom: 6px;
               position: relative;
               width: 100%;
               height: 100%;
               bottom: 0px;
               top: 0px;
               border-radius: 10px;
               padding-left: 20px;

               ">
               <p style="font-weight: 900;">库位释放规则</p>
                 <el-form-item label="质检库位释放" prop="warehouseReleaseRule"  label-width="180px"
               >
                 出厂生产后  <el-input-number size="mini" v-model="form.warehouseReleaseRule" :min="0"  ></el-input-number> 小时提醒：如果填0表示出场生成快递单后就会立刻释放

              </el-form-item>

               </div>

           </el-col>   -->

          </el-row>



        <el-row>

          <el-col :span="12">
          <el-form-item label="洗护正常时效" prop="normalWashingDuration">
            <el-input-number v-model="form.normalWashingDuration" :min="1" ></el-input-number>
           </el-form-item>
         </el-col>

          <el-col :span="12">
           <el-form-item label="洗护逾期时效" prop="expirationDateForCleaning">
          <!-- <el-input v-model="form.expirationDateForCleaning" placeholder="请输入洗护逾期时效 单位分" /> -->
            <el-input-number v-model="form.expirationDateForCleaning" :min="1" ></el-input-number>
        </el-form-item>
        </el-col>
        </el-row>


         <el-row>
         <el-col :span="12">
              <el-form-item label="工厂日均产能" prop="capacityOfProd">
          <!-- <el-input v-model="form.dailyCapacity" placeholder="请输入日均产能" /> -->
          <el-input-number v-model="form.capacityOfProd" :min="1" ></el-input-number>
        </el-form-item>
        </el-col>

         </el-row>








          <el-col :span="12">
           <el-form-item label="服务类型" prop="serviceType">

             <el-select clearable v-model="form.serviceType"  multiple  placeholder="请选择"  style="width:100%">
				           <el-option
                          v-for="item in serviceTypeList"
                          :key="item.code"
                          :label="item.name"
                          :value="item.code">
            </el-option>

				</el-select>
        </el-form-item>
        </el-col>

         <el-col :span="12">
           <el-form-item label="状态" prop="status">
             <!-- <el-input v-model="form.status" placeholder="请选择" /> -->
               <el-radio v-model="form.status" label="正常营业">启用</el-radio>
               <el-radio v-model="form.status" label="暂停营业">禁用</el-radio>
                <div style="color: gray;">注意：禁用状态不进行派单，并且在派单规则不再显示</div>
        </el-form-item>

        </el-col>


        <!-- <el-col :span="12">

          <el-form-item label="" prop="status">

        </el-form-item>
        </el-col> -->

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"    :loading="submitLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFactory, getFactory, delFactory, addFactory, updateFactory ,getFactoryType , getServiceType ,getFactoryDetailById } from "@/api/factory/index";
import { Row } from "element-ui";
import { regionData , codeToText } from "element-china-area-data";
import { status } from "nprogress";
export default {
  name: "Factory",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
       selectedOptions: [],
       submitLoading:false,
      // 总条数
      total: 0,
      options: regionData,
      // 【请填写功能名称】表格数据
      factoryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      factoryTypeList:[],

      serviceTypeList:[],


      // 查询参数
      queryParams: {
        pageNum: 1,
        pageNo:1,
        pageSize: 10,
        factoryIcon: null,
        factoryManager: null,
        factoryName: null,
        factoryCode: null,
        factoryPhone: null,
        warehouseReleaseRule: 1,
        dailyCapacity: null,
        expirationDateForCleaning: null,
        normalWashingDuration: null,
        serviceType: null,
        factoryType: null,
        factoryAddress: null,
        status: null
      },
      // 表单参数
      form: {
         factoryPhone:17125025693,
         factoryName:"小黑",
         capacityOfProd:1
      },
      // 表单校验
      rules: {
        // factoryIcon: [
        //   { required: true, message: "工厂图标不能为空", trigger: "blur" }
        // ],

         serviceType: [
          { required: true, message: "服务类型不能为空", trigger: "blur" }
        ],

        warehouseReleaseRule: [

           { required: true, message: "库释放配置不能为空", trigger: "blur" }

        ],

        //
         status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],

        factoryType:[
          { required: true, message: "工厂类型不能为空", trigger: "blur" }
        ],


        factoryManager: [
          { required: true, message: "工厂负责人不能为空", trigger: "blur" }
        ],
        factoryName: [
          { required: true, message: "工厂名字不能为空", trigger: "blur" }
        ],
        factoryCode: [
          { required: true, message: "工厂编号不能为空", trigger: "blur" }
        ],
        factoryPhone: [
          { required: true, message: "工厂联系电话不能为空", trigger: "blur" },
           {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },

        ],

       /* city:[
          { required: true, message: "选择地址不能为空", trigger: "blur" }
        ],*/
        factoryAddress: [
          { required: true, message: "工厂地址不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建者不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "更新者不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getInitData()
  },
  methods: {

     handleChange() {
      this.form.provinces = this.selectedOptions[0];
       this.form.city = this.selectedOptions[1];
      this.form.country = this.selectedOptions[2];
    },
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listFactory(this.queryParams).then(response => {
        this.factoryList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        capacityOfProd:1,
        factoryIcon: null,
        factoryManager: null,
        factoryName: null,
        factoryCode: null,
        factoryPhone: null,
        warehouseReleaseRule: null,
        dailyCapacity: null,
        expirationDateForCleaning: null,
        normalWashingDuration: null,
        serviceType: null,
        factoryType: null,
        factoryAddress: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        status: null
      };
      this.selectedOptions=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },

   getInitData(){
       getFactoryType({}).then(res=>{
         this.factoryTypeList=res.data
      })

       getServiceType({}).then(res=>{
         this.serviceTypeList=res.data
      })


   },

   handleStatus(row,status){

      addFactory({id:row.id,status:status}).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
        });



   },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增工厂";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const id = row.id || this.ids
      getFactoryDetailById({id}).then(response => {
        this.form = response.data;
        if(this.form.serviceType){
           this.form.serviceType=this.form.serviceType.map(element => {
            return element.code
        });
        }

         this.selectedOptions = [
          this.form.provinces,
           this.form.city,
          this.form.country,
        ];
        this.open = true;
        this.title = "修改工厂";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.submitLoading=true
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form ={
                      ... this.form,
                    cityName: codeToText[this.form.city],
                    provincesName: codeToText[this.form.provinces],
                    countryName: codeToText[this.form.country],
                    status:this.form.status=="正常营业"? "NORMAL":"STOP"
            }
          if (this.form.id != null) {

            addFactory(form).then(response => {
              this.submitLoading=false
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFactory(form).then(response => {
                 this.submitLoading=false
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
           this.reset()
        }
        this.submitLoading=false
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项？').then(function() {
        return delFactory(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('factory/export', {
        ...this.queryParams
      }, `工厂表导出_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.warehouseReleaseRule{
     height: 100px;
    display: flex;
    position: relative;
    padding-top: 10px;
    padding-bottom: 2px;
    padding-right: 5px;
    padding-left: 3px;
    flex-direction: row;
    justify-items: center;
    align-items: center;
    /* justify-content: center; */
}

/* label{

  font-weight: 200 !important;
} */
</style>
